package com.hliushi.learn.service.impl;

import com.hliushi.learn.service.RedisService;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName RedisServiceImpl
 * @Description TODO
 * @Author liushi
 * @Date 2021/7/8 22:22
 * @Version V1.0
 **/
@Service
public class RedisServiceImpl implements RedisService {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * set redis: hash类型
     * #
     * hash类型其实原理和string一样的, 但是有两个key, 使用stringRedisTemplate.opsForHash()
     * 可以获取HashOperation<String, Object, Object>对象
     *
     * @param key      key
     * @param filedKey filedKey
     * @param value    value
     */
    @Override
    public void setHash(String key, String filedKey, String value) {
        HashOperations<String, Object, Object> hashOperations = stringRedisTemplate.opsForHash();
        hashOperations.put(key, filedKey, value);
    }


    /**
     * get redis: hash类型
     *
     * @param key      key
     * @param filedKey filedKey
     * @return
     */
    @Override
    public String getHash(String key, String filedKey) {
        /**
         * 接口HashOperation
         * #
         * public interface HashOperations<H, HK, HV>
         * HV get(H key, Object hashKey);
         */

        return (String) stringRedisTemplate.opsForHash().get(key, filedKey);
    }

    /**
     * set redis: string类型
     *
     * @param key   key
     * @param value value
     */
    @Override
    public void setString(String key, String value) {
        ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
        valueOperations.set(key, value);
    }


    /**
     * get redis: string类型
     *
     * @param key key
     * @return
     */
    @Override
    public String getString(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    /**
     * set redis: list类型
     *
     * @param key   key
     * @param value value
     * @return
     */
    @Override
    public long setList(String key, String value) {
        ListOperations<String, String> listOperations = stringRedisTemplate.opsForList();
        return listOperations.leftPush(key, value);
    }

    /**
     * get redis: list类型
     * #
     * stringRedisTemplate.opsForList()可以获取ListOperations<String, String> listOperations redis列表对象
     * 该对象是个简单的字符串列表, 可以支持从左侧添加, 也可支持从右侧添加
     * 一个列表最多包含2 ^ 32 - 1 个元素
     *
     * @param key   key
     * @param start start
     * @param end   end
     * @return
     */
    @Override
    public List<String> getList(String key, long start, long end) {
        ListOperations<String, String> listOperations = stringRedisTemplate.opsForList();
        return listOperations.range(key, start, end);
    }
}
